let vData=[
  {
    "card_type": "BANNER",
    "item": {},
    "items": [{
      "image": {
        "width": 120,
        "height": 100,
        "url": "http://www.xx.xx/a.jpg"
      },
      "action": {
        "intent": "weiyou://category/xxx",
        "title": "游戏陪练",
      }
    }],
    "action": {}
  },
  {
    "card_type": "SECTION",
    "item": {
      "title": "情绪发泄"
    },
    "items": [{
      "nick": "章子怡",
      id:'001',
      "avatar": {
        "url": "http://img1.imgtn.bdimg.com/it/u=3121220405,3506568078&fm=27&gp=0.jpg",
        "height": 120,
        "width": 120
      },
      "audio": {
        "url": "http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46",
        "duration": 10000
      },
      "price": 20,
      "unit": 1.0,
      "heat_rate": 5,
      "badge": "NEW",
      "action": {
        "intent": "weiyou://"
      }
    },
      {
        "nick": "范冰冰",
        id: '002',
        "avatar": {
          "url": "http://img5.imgtn.bdimg.com/it/u=3676239104,3543605203&fm=27&gp=0.jpg",
          "height": 120,
          "width": 120
        },
        "audio": {
          "url": "http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46",
          "duration": 8000
        },
        "price": 35,
        "unit": 1.0,
        "heat_rate": 5,
        "badge": "NEW",
        "action": {
          "intent": "weiyou://"
        }
      },
    ],
    "action": {
      "intent": "weiyou://xxx",
      "title": "情感陪伴"
    }
  }
];
let has_next=false;
Page({
  data: {
    anchorsList:[],
    title:'',
    hasNext:false,
  },


  onLoad: function (options) {
    let anchorInfo = vData.find(item => item.card_type ==='SECTION');

    anchorInfo.items.forEach(item=>{
      item.audio.duration_res = item.audio.duration/1000;
      item.audio.isPlay=true;
    })

    this.setData({
      anchorsList: anchorInfo.items,
      title: anchorInfo.item.title,
    })
  },

  toDetail(e){
    let type=e.target.dataset.type;
    wx.navigateTo({
      url:`../anchors/anchors?type=${type}`,  
      success:function() {
      },        
    })
  },

  audioPlay(e){
    let id = e.currentTarget.dataset.id;
    let anchor = this.data.anchorsList.find(item=>item.id==id);
    anchor.isPlay = !anchor.isPlay;
    console.log(anchor.isPlay);
    const innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.autoplay = true
    innerAudioContext.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
    for (let i = 0; i < this.data.anchorsList.length;i++){
      if (this.data.anchorsList[i].id==id){
          this.data.anchorsList[i] = anchor;
          break;
        }
    }
    this.setData({
      anchorsList: this.data.anchorsList,
    })

    if (anchor.isPlay){
      innerAudioContext.onPlay(() => {
        console.log('开始播放')
      })
    }else{
      innerAudioContext.pause()
    }
   
    
  },

  seeMoreClick(){
    if (!this.data.hasNext){
      wx.showToast({
        title: '暂无更多数据',
        icon:'loading',
        duration: 1000,
        mask: true
      })
    }
  },

  toSkillDetail(e){
    wx.navigateTo({
      url: `../skillDetail/skillDetail?id=${e.currentTarget.dataset.id}`,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})